<?php
!defined ('P_W') && exit('Forbidden');

class Pw_House {

	var $_hid;
	var $info;
	
	function Pw_House() {
		$this->__construct();
	}
	
	function __construct() {
		
	}
	
	function setHid($hid){
		$this->_hid = $hid;
	}
	
	function init($extra = false){
		$this->info = $this->getHouseInfoByHid($this->_hid,$extra);
		return !empty($this->info);
	}

	function setNav() {
		if (!$this->info['nav'] || !is_array($navdb = unserialize($this->info['nav']))) {
			$this->info['nav'] = array();
		} else {
			foreach ($navdb as $key => $value) {
				$a_style = $fontstyle = '';
				$style_array = explode("|", $value['style']);
				$style_array[0] && $fontstyle .= "color:{$style_array[0]};";
				$style_array[1] && $fontstyle .= "font-weight:bolder;";
				$style_array[2] && $fontstyle .= "font-style:italic;";
				$style_array[3] && $fontstyle .= "text-decoration:underline;";
				$fontstyle && $a_style .= " style=\"$fontstyle\"";
				$value['target'] && $a_style .= ' target="_blank"';
				$value['alt'] && $a_style .= " title=\"{$value['alt']}\"";
				$navdb[$key]['a_style'] = $a_style;
			}
			$this->info['nav'] = $navdb;
		}
	}

	function setBanner() {
		$banner_img = '';
		if ($this->info['banner']) {
			list($banner_img, $banner_type) = geturl($this->info['banner'], 'lf');
		}
		!$banner_img && $banner_img = 'mode/house/images/house.jpg';
		$this->info['banner'] = $banner_img;
	}

	function updateHits() {
		$houseInfoDB = $this->_getHouseInfoDB();
		$houseInfoDB->updateHits($this->_hid);
	}

	function getFeedNum() {
		$houseFeedDB = $this->_getHouseFeedDB();
		return $houseFeedDB->getCountByHid($this->_hid);
	}
	
	function getEvaluntionNum(){
		$houseEvaluntionDB = $this->_getEvaluationDB();
		return $houseEvaluntionDB->getEvaluationCountByHid($this->_hid,true);
	}
	
	function getDimDate($date){
		if(date('d', $date) < 10) $d = '上旬';
		if(date('d', $date) >= 10 && date('d', $date) <= 20) $d = '中旬';
		if(date('d', $date) > 20) $d = '下旬';
		return date('Y',$date).'年'.date('n',$date).'月'.$d;
	}

	function getFeed($offset = 0, $limit = 20) {
		$houseFeedDB = $this->_getHouseFeedDB();
		$info = $houseFeedDB->getByHid($this->_hid, $offset, $limit);
		foreach ($info as $key => $value) {
			$info[$key]['postdate'] = get_date($value['postdate'], 'Y-m-d');
			$info[$key]['descrip'] = str_replace("\n", '<br />', $value['descrip']);
		}
		return $info;
	}

	function getDeveloper() {
		if (!$this->info['developer']) return array();
		$houseDeveloperDB = $this->_getHouseDeveloperDB();
		if ($info = $houseDeveloperDB->getById($this->info['developer'])) {
			$info['introduction'] = str_replace("\n", '<br />', $info['introduction']);
		}
		return $info;
	}

	function getBitValue($bit, $vkey, $implode = '') {
		if (!$bit) return '';
		global $house_field;
		$retv = array();
		foreach ($house_field[$vkey] as $key => $value) {
			if ($bit & (1 << ($key-1))) {
				$retv[] = $value['name'];
			}
		}
		return $implode ? implode($retv, $implode) : $retv;
	}
	
	function getHouseInfoByHid($hid,$extra = false){
		if(intval($hid) <= 0){
			return array();
		}
		$houseInfoDB = $this->_getHouseInfoDB();
		$houseInfo = array();
		if($extra){
			$houseInfo = $houseInfoDB->getAllInfoById($hid);
		}else{
			$houseInfo = $houseInfoDB->getInfoById($hid);
		}
		return $houseInfo;
	}
	
	function _getHouseInfoDB() {
		return House::loadDB('HouseInfo');
	}
	
	function _getHouseFeedDB(){
		return House::loadDB('HouseFeed');
	}
	
	function _getHouseDeveloperDB(){
		return House::loadDB('Developer');
	}
	
	function _getEvaluationDB() {
		return House::loadDB('Evaluation');
	}
}
?>